<?

/////////////// CLASSES /////////////

class CN {
	function CN($raw) {
		$this->_addvars( is_string($raw) ? json_decode($raw): $raw);
		$this->raw = is_string($raw)? $raw: json_encode($raw);
	}
	
	function _addvars($vars) { foreach($vars as $k=>$var) $this->$k = $var; }

	function _pregmatch($text) {
		$text = stripslashes($text);
		if(preg_match_all('/(<%)([^%]*)(%>)/',$text, $raw, PREG_SET_ORDER)){
			$replace = $replace_by = array();
			foreach($raw as $v) if(!in_array($v[0],$replace)){
				$replace[]=$v[0];
				$replace_by[] = $this->_inline($v[2]);
			}
			$text = str_replace($replace, $replace_by, $text);
			
		}
		return $text;
	}
	
	function _pregreset($text, $vars) {
		$text = stripslashes($text);
		if(preg_match_all('/(<div)([^%]*)(/div>)/',$text, $raw, PREG_SET_ORDER)){
			$replace = $replace_by = array();
			foreach($raw as $v) if(!in_array($v[0],$replace)){
				//$replace[]=$v[0];
				//$v[2]=json_decode($v[2],true);
				//$replace_by[] = $this->_inline(key($v[2]), current($v[2]));
			}
			$text = str_replace($replace, $replace_by, $text);
		}
		return $text;
	}
	
	function _inline($mod_id) {
		global $DB;
		$raw = json_decode($DB->short("json","moddump","ID=$mod_id"));
		$_set = new CN(current($raw));
		$_tool	= array(key($raw), $_set->tool);
		return include ROOT."control.room/mod.".key($raw).".php";
		//return "";
	}
	
	function _unline($var, $meta) {
		//$_tool	= array($var, $meta->tool);
		//$_set = new CN($meta);
		//return include ROOT."control.room/mod.$var.php";
	}
	
	public $tablederivs	= array("page"=>"pages", "mod"=>"moddump", "text"=>"txtdump", "link"=>"db_link", "stream"=>"db_media", "group"=>"mod_object");
	public $sqlsugar	= array("img"=>"(SELECT sg.objid FROM glue_object as sg WHERE sg.parentype=%0 AND sg.objtype=%1 AND sg.parentid=%2) as img", "descr"=>"(SELECT st.long FROM txtdump as st WHERE st.textid=%0 AND st.lang='LANG' AND st.active=1) as descr");
	
	///////////////// EXECUTES /////////////////
	// Shift execute
	function shift() {
		global $DB;
		$meta = &$this->vars->meta;
		
		if(! $table = $this->tablederivs[$this->vars->type]) $table = "mod_".$this->vars->type;
		$value = ($meta->options)? $meta->options[(int)$meta->value] : (int)$meta->value;
		
		if(strpos($meta->field,".")) {
			$field = explode(".", $meta->field);
			$meta->field = array_shift($field);
			$json = json_decode(($jsonraw = $DB->short($meta->field, $table, "ID=".$meta->objid))? $jsonraw : "{}");
			foreach($field as $n => $fld) {
				if($n==count($field)-1) if(!$digger) $json->{$fld} = $value; else $digger->{$fld} = $value;
				else if($digger) $digger = &$digger->{$fld};
				else $digger = &$json->{$fld};
			}
			$value = json_encode($json);
		}
		$DB->tube("UPDATE $table SET ".$meta->field."='$value' WHERE ID=".$meta->objid);
		return $meta->objid;
	}
	
	// Ajx field execute
	function ajax() {
		global $DB;
		$meta = &$this->vars->meta;
		
		if(! $table = $this->tablederivs[$this->vars->type]) $table = "mod_".$this->vars->type;
		$value = ($meta->options)? $meta->options[(int)$meta->value] : $meta->value;
		
		if(strpos($meta->field,".")) {
			$field = explode(".", $meta->field);
			$meta->field = array_shift($field);
			$json = $DB->parse_json($meta->objid, $table, $meta->field);//json_decode(($jsonraw = $DB->short($meta->field, $table, "ID=".$meta->objid))? $jsonraw : "{}");
			foreach($field as $n => $fld) {
				if($n==count($field)-1) if(!$digger) $json->{$fld} = $value; else $digger->{$fld} = $value;
				else if($digger) $digger = &$digger->{$fld};
				else $digger = &$json->{$fld};
			}
			$value = json_encode($json);
		}
		$DB->tube("UPDATE $table SET ".$meta->field."='$value' WHERE ID=".$meta->objid);
		return $meta->objid;
	}
	
	// Pull execute
	function pull() {
		global $DB;
		$meta = &$this->vars->meta;
		if(! $table = $this->tablederivs[$this->vars->type]) $table = "mod_".$this->vars->type;
				
		if( $objects = $DB->parse("ID, ".(($meta->field == "name")?$DB->_lang("nameid")." as name":$meta->field), $table, "type=".$meta->objtype, true))
			foreach($objects as $k=>$obj)
				$objects[$k] = "{id:".$obj['ID'].", type:'".$this->vars->type."', tool:'".$this->vars->type.".basic', name:'".strlimit($obj[$meta->field], 20)."', descr:'', list:[\"".$obj[$meta->field]."\"], objtype:".$meta->objtype.", local:'".$this->vars->id."', meta:{}}";
			
		return ($objects)? implode(", ", $objects): 0;
	}

}
	





?>